﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CommandProcessor.Dispatcher;
using Data.Models;
using Data.Repositories;
using Microsoft.Reporting.WebForms;
using QuanLySanXuat.Models;

namespace QuanLySanXuat.Controllers
{
    public class PriceStoreController : Controller
    {
        private readonly ICommandBus commandBus;
        private readonly IStockRepository stockRepository;
        private readonly IPriceStoreRepository pricStoreRepository;
        public PriceStoreController(ICommandBus commandBus, IStockRepository stockRepository, IPriceStoreRepository pricStoreRepository)
        {
            this.commandBus = commandBus;
            this.stockRepository = stockRepository;
            this.pricStoreRepository = pricStoreRepository;
        }
        //
        // GET: /PriceStore/

        public ActionResult Index()
        {
            ViewData["Stock"] = stockRepository.GetAll();
            return View();
        }
        public ActionResult PrintView(int ID)
        {
            
            var parameter = new SqlParameter
            {
                DbType = DbType.Int32,
                ParameterName = "@stockID",
                Value = ID
            };

            var priceStore = pricStoreRepository.ExecWithStoreProcedure
                                         ("PriceStore @stockID ",
                                            parameter
                                         ).ToList();

            return PartialView("_PriceStoreList", priceStore);
        }
        public FileContentResult Print(int ID)
        {

            LocalReport localReport = new LocalReport();
            localReport.ReportPath = Server.MapPath("~/Report/Reports/RptPriceStore.rdlc");

            var parameter = new SqlParameter
            {
                DbType = DbType.Int32,
                ParameterName = "@stockID",
                Value = ID
            };

            var priceStore = pricStoreRepository.ExecWithStoreProcedure
                                         ("PriceStore @stockID ",
                                            parameter
                                         ).ToList();


            localReport.DataSources.Clear();
            ReportDataSource reportDataSource = new ReportDataSource("Ds", priceStore);
            localReport.DataSources.Add(reportDataSource);
            localReport.Refresh();


            string reportType = "PDF";
            string mimeType;
            string encoding;
            string fileNameExtension;
            //The DeviceInfo settings should be changed based on the reportType            
            //http://msdn2.microsoft.com/en-us/library/ms155397.aspx            
            string deviceInfo = "<DeviceInfo>" +
                "  <OutputFormat>jpeg</OutputFormat>" +
                "  <PageWidth>8.5in</PageWidth>" +
                "  <PageHeight>11in</PageHeight>" +
                "  <MarginTop>0.5in</MarginTop>" +
                "  <MarginLeft>1in</MarginLeft>" +
                "  <MarginRight>1in</MarginRight>" +
                "  <MarginBottom>0.5in</MarginBottom>" +
                "</DeviceInfo>";
            Warning[] warnings;
            string[] streams;
            byte[] renderedBytes;
            //Render the report            
            renderedBytes = localReport.Render(reportType, deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);
            //Response.AddHeader("content-disposition", "attachment; filename=NorthWindCustomers." + fileNameExtension); 
            return File(renderedBytes, "pdf");
        }
    }
}
